package com.utils.web.jdbc;

import java.sql.*;
import java.util.Map;


public class MySQLConnect {
	 
    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
    static final String DB_URL = "jdbc:mysql://localhost:9654/joke";
    static final String USER = "joke";
    static final String PASS = "1234";
    
    static Connection conn = null;
    static Statement stmt = null;
    static ResultSet rs = null;
    static PreparedStatement prestmt = null;
    
    public static Connection getConnect() {
       
        try {
        	 // 注册 JDBC 驱动
        	System.out.println("连接数据库...");
			Class.forName("com.mysql.jdbc.Driver");
			// 打开链接
			conn = DriverManager.getConnection(DB_URL,USER,PASS);
			// 执行查询
			stmt = conn.createStatement();
			System.out.println("连接数据库成功");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
        return conn;
    }
    
    
    public static void insertUser(Map<String,String> map) {
    	getConnect();
        try{
            String sql = "";
            sql = " INSERT INTO yi_user " + 
            		" (username,password,email,sex,qq,phone,level,money,avatar,audit_num,audit_suc_num,audit_pro,send_num,register_ip,last_login_ip,last_login_time,created_time,online_time,status,openid,sign_time,follows,fans) " + 
            		" VALUES ( ?, '', '', '2', null, null, '1', '0', ?, '0', '0', '0', '0', '127.0.0.1', '127.0.0.1', '0', '0', '0', '1', null, '0', '0', '0')";
            prestmt = conn.prepareStatement(sql);
            System.out.println("保存数据...");
			for (String str : map.keySet()) {
				prestmt.setString(1,str);
            	prestmt.setString(2,map.get(str));
				prestmt.addBatch();
			}
			prestmt.executeBatch();
            System.out.println("保存数据完成"); 
        }catch(SQLException se){
            // 处理 JDBC 错误
            se.printStackTrace();
        }catch(Exception e){
            // 处理 Class.forName 错误
            e.printStackTrace();
        }finally{
            // 完成后关闭资源
        	try {
        		if(rs!=null) rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
        	
            try{
                if(stmt!=null) stmt.close();
            }catch(SQLException se2){
            } 
            
            try{
                if(prestmt!=null) prestmt.close();
            }catch(SQLException se2){
            } 
            
            try{
                if(conn!=null) conn.close();
            }catch(SQLException se){
                se.printStackTrace();
            }
        }
    
    }
    
    
    public void query() {
    	getConnect();
        try{
            String sql;
            sql = " ";
            
            rs = stmt.executeQuery(sql);
            
            // 展开结果集数据库
            while(rs.next()){
                // 通过字段检索
                int id  = rs.getInt("id");
                String name = rs.getString("name");
                String url = rs.getString("url");
    
                // 输出数据
                System.out.print("ID: " + id);
                System.out.print(", 站点名称: " + name);
                System.out.print(", 站点 URL: " + url);
                System.out.print("\n");
            }
          
        }catch(SQLException se){
            // 处理 JDBC 错误
            se.printStackTrace();
        }catch(Exception e){
            // 处理 Class.forName 错误
            e.printStackTrace();
        }finally{
            // 完成后关闭资源
        	try {
        		if(rs!=null) rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
        	
            try{
                if(stmt!=null) stmt.close();
            }catch(SQLException se2){
            } 
            
            try{
                if(conn!=null) conn.close();
            }catch(SQLException se){
                se.printStackTrace();
            }
        }
    
    
    }

}
